package com.stylefeng.guns.rest.modular.wz.dao;

import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.enums.EnWzAdv;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserFinanceRecordService;
import com.stylefeng.guns.rest.modular.wz.service.IWzUserService;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzUserAdvServiceImpl;
import com.stylefeng.guns.rest.modular.wz.service.impl.WzUserCouponServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 资金数据保存
 **/
@Service
public class WzFinanceDao {

    @Autowired
    private WzUserMapper userMapper;
    @Autowired
    private WzUserFinanceRecordMapper userFinanceRecordMapper;
    @Autowired
    private WzUserCouponMapper userCouponMapper;
    @Autowired
    private WzMapActivityMapper mapActivityMapper;
    @Autowired
    private WzCouponMapper couponMapper;
    @Autowired
    private WzCouponOrderMapper couponOrderMapper;
    @Autowired
    private WzAdvMapper advMapper;
    @Autowired
    private WzUserAdvRecordMapper userAdvRecordMapper;
    @Autowired
    private WzAdvMapMapper advMapMapper;
    @Autowired
    private PfFinanceRecordMapper pfFinanceRecordMapper;
    @Autowired
    private WzUserAdvServiceImpl userAdvService;
    @Autowired
    private WzMapMergeMapper mapMergeMapper;
    @Autowired
    private WzUserMergeMapper userMergeMapper;
    @Autowired
    private WzUserMergeDetailMapper userMergeDetailMapper;
    @Autowired
    private WzUserCouponServiceImpl userCouponService;
    @Autowired
    private WzUserRebateMapper userRebateMapper;
    @Autowired
    private WzMapRebateMapper mapRebateMapper;
    @Autowired
    private WzRebateShareMapper rebateShareMapper;
    @Autowired
    private WzCouponMarketMapper couponMarketMapper;


    @Transactional
    public boolean couponScan(WzUser user, WzUserFinanceRecord userFinanceRecord,WzUserCoupon userCoupon,WzUserFinanceRecord marketUserFinanceRecord,WzUser marketUser) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = userCouponMapper.updateById(userCoupon);
        }
        if(r > 0){
            if(marketUser != null){
                r = userMapper.updateById(marketUser);
            }
        }
        if(r > 0){
            if(marketUserFinanceRecord != null){
                r = userFinanceRecordMapper.insert(marketUserFinanceRecord);
            }
        }
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean addMapActivity(WzMapActivity mapActivity, WzCoupon coupon, WzUserFinanceRecord userFinanceRecord, WzUser user) {
        int r = userMapper.updateById(user);
        if (r > 0) {
            r = couponMapper.insert(coupon);
            if (r > 0) {
                r = mapActivityMapper.insert(mapActivity);
                if (r > 0) {
                    r = userFinanceRecordMapper.insert(userFinanceRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean mapActivityCheck(WzUser user, WzUserFinanceRecord userFinanceRecord, WzMapActivity activity) {
        int  r = userMapper.updateById(user);
        if (r > 0) {
            r  = mapActivityMapper.updateById(activity);
            if (r > 0) {
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean buyCoupon(WzUser user, WzUserFinanceRecord userFinanceRecord, WzCouponOrder couponOrder, int type) {
        int  r = userMapper.updateById(user);
        if (r > 0) {
            if(type == 1){
                r = couponOrderMapper.insert(couponOrder);
            }else if(type == 2){
                r = couponOrderMapper.updateById(couponOrder);
            }
            if (r > 0) {
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean advReward(WzAdv adv, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserAdvRecord advRecord) {
        int r = advMapper.updateById(adv);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userFinanceRecord);
                if(r > 0){
                    r = userAdvRecordMapper.insert(advRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean editMapActivity(WzMapActivity mapActivity, WzCoupon coupon, WzUserFinanceRecord userFinanceRecord, WzUser user) {
        int r = userMapper.updateById(user);
        if (r > 0) {
            r = couponMapper.updateById(coupon);
            if (r > 0) {
                r = mapActivityMapper.updateById(mapActivity);
                if (r > 0) {
                    r = userFinanceRecordMapper.insert(userFinanceRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean advMapAdd(WzAdvMap advMap, WzUser user, WzUserFinanceRecord userFinanceRecord) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = advMapMapper.insert(advMap);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean saveAdvMapCheckFail(WzAdvMap advMap, WzUser user, WzUserFinanceRecord userFinanceRecord) {
        int r = advMapMapper.updateById(advMap);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean recharge(WzUser user, WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord) {
        int r = userFinanceRecordMapper.updateById(userFinanceRecord);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = pfFinanceRecordMapper.insert(pfFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean repackedReward(WzAdv adv, WzUser user, WzUserFinanceRecord userFinanceRecord, WzUserAdvRecord advRecord) {
        int r = advMapper.updateById(adv);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = userAdvRecordMapper.insert(advRecord);
                if(r > 0){
                    r = userFinanceRecordMapper.insert(userFinanceRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean renewalsAdvMap(WzAdvMap advMap, WzUserFinanceRecord userFinanceRecord, WzUser user) {
        int r = advMapMapper.updateById(advMap);
        if (r > 0) {
            r = userMapper.updateById(user);
            if (r > 0) {
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean saveSetTopAdvMap(WzUserFinanceRecord userFinanceRecord, WzUser user, WzAdv adv, WzAdvMap advMap, List<WzUserAdv> userAdvs) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = advMapMapper.updateById(advMap);
        }
        if(r > 0){
            if(adv != null){
                r = advMapper.insert(adv);
                if(r > 0){
                    r = userFinanceRecordMapper.insert(userFinanceRecord);
                }
            }
        }
        if(r > 0){
            boolean f = userAdvService.insertBatch(userAdvs);
            if(f){
                r = 1;
            }else{
                r = 0;
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean createMerge(WzUserMerge userMerge, WzUserFinanceRecord userFinanceRecord ,WzUserMergeDetail userMergeDetail) {
        int r = userMergeMapper.insert(userMerge);
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
            if(r > 0) {
                r = userMergeDetailMapper.insert(userMergeDetail);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Transactional
    public boolean mapMergeCallback(WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord, WzUserMergeDetail userMergeDetail, WzUserMerge userMerge) {
        int r = userMergeMapper.updateById(userMerge);
        if(r > 0){
            r = userMergeDetailMapper.updateById(userMergeDetail);
            if(r > 0){
                r = userFinanceRecordMapper.updateById(userFinanceRecord);
                if(r > 0){
                    r = pfFinanceRecordMapper.insert(pfFinanceRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean originalPriceBuy(WzUserFinanceRecord userFinanceRecord,WzCouponOrder couponOrder) {
        int r = userFinanceRecordMapper.insert(userFinanceRecord);
        if(r > 0){
            r = couponOrderMapper.insert(couponOrder);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean couponOrderCashCallback(WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord, List<WzUserCoupon> userCoupons,WzCouponOrder couponOrder) {
        int r = userFinanceRecordMapper.updateById(userFinanceRecord);
        if(r > 0){
            r = couponOrderMapper.updateById(couponOrder);
            if(r > 0){
                r = pfFinanceRecordMapper.insert(pfFinanceRecord);
                if(r > 0){
                    boolean f = userCouponService.insertBatch(userCoupons);
                    if(f){
                        r = 1;
                    }else{
                        r = 0;
                    }
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean joinMergeCashReward(WzUserMerge userMerge,WzUserFinanceRecord userFinanceRecord, WzUserMergeDetail userMergeDetail, WzUserFinanceRecord userRewardFinanceRecord, WzUser user) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = userMergeDetailMapper.insert(userMergeDetail);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userRewardFinanceRecord);
                if(r > 0){
                    r = userMergeMapper.insert(userMerge);
                }
            }
        }
        if(r > 0){
            if(userFinanceRecord != null) {
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean joinMergeCash(WzUserFinanceRecord userFinanceRecord, WzUserMergeDetail userMergeDetail) {
        int r = userMergeDetailMapper.insert(userMergeDetail);
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean joinMergeCashRewardMyCoupon(WzUserMerge userMerge, WzUserMergeDetail userMergeDetail, WzUserFinanceRecord userRewardFinanceRecord, WzUser user, List<WzUserCoupon> userCoupons,WzMapMerge mapMerge) {
        int r = userMergeMapper.updateById(userMerge);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userRewardFinanceRecord);
                if(r > 0){
                    r = userMergeDetailMapper.insert(userMergeDetail);
                    if(r > 0){
                        r = mapMergeMapper.updateById(mapMerge);
                        if(r > 0){
                            boolean f = userCouponService.insertBatch(userCoupons);
                            if(f){
                                r = 1;
                            }else{
                                r = 0;
                            }
                        }
                    }
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean joinMergeCashRewardAllCoupon(WzUserMerge userMerge,WzUserMergeDetail userMergeDetail,WzUserMergeDetail createUserMergeDetail, WzUserFinanceRecord userRewardFinanceRecord, WzUser user, List<WzUserCoupon> userCoupons, List<WzUserCoupon> userCouponCreates,WzMapMerge mapMerge) {
        int r = userMergeMapper.updateById(userMerge);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = userMergeDetailMapper.updateById(createUserMergeDetail);
                if(r > 0){
                    r = userFinanceRecordMapper.insert(userRewardFinanceRecord);
                    if(r > 0){
                        r = userMergeDetailMapper.insert(userMergeDetail);
                        if(r > 0){
                            r = mapMergeMapper.updateById(mapMerge);
                            if(r > 0){
                                boolean f = userCouponService.insertBatch(userCoupons);
                                if(f){
                                    f = userCouponService.insertBatch(userCouponCreates);
                                    if(f){
                                        r = 1;
                                    }else{
                                        r = 0;
                                    }
                                }else {
                                    r = 0;
                                }
                            }
                        }
                    }
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean createMergeCashReward(WzUserMerge userMerge, WzUserFinanceRecord userFinanceRecord, WzUserMergeDetail userMergeDetail, WzUserFinanceRecord userRewardFinanceRecord, WzUser user) {
        int r = userMapper.updateById(user);
        if(r > 0){
            r = userMergeDetailMapper.insert(userMergeDetail);
            if(r > 0){
                r = userFinanceRecordMapper.insert(userRewardFinanceRecord);
                if(r > 0){
                    r = userMergeMapper.insert(userMerge);
                }
            }
        }
        if(r > 0){
            if(userFinanceRecord != null){
                r = userFinanceRecordMapper.insert(userFinanceRecord);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Transactional
    public boolean joinMapMergeCallback(WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord,WzUserMerge userMerge, WzUserMergeDetail userMergeDetail, WzUserMergeDetail createUserMergeDetail, List<WzUserCoupon> userCoupons, List<WzUserCoupon> createUserCoupons, WzMapMerge mapMerge) {
        int r = userFinanceRecordMapper.updateById(userFinanceRecord);
        if(r > 0){
            r = mapMergeMapper.updateById(mapMerge);
            if(r > 0){
                r = userMergeMapper.updateById(userMerge);
                if(r > 0){
                    r = userMergeDetailMapper.updateById(createUserMergeDetail);
                    if(r > 0){
                        if(r > 0){
                            r = userMergeDetailMapper.updateById(userMergeDetail);
                            if(r > 0){
                                boolean f = userCouponService.insertBatch(userCoupons);
                                if(f){
                                    r = pfFinanceRecordMapper.insert(pfFinanceRecord);
                                    if(createUserCoupons != null){
                                        f = userCouponService.insertBatch(createUserCoupons);
                                        if(f){
                                            r = 1;
                                        }else{
                                            r = 0;
                                        }
                                    }
                                }else{
                                    r = 0;
                                }
                            }
                        }
                    }
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean createUserRebate(WzUserRebate userRebate, WzUserFinanceRecord userFinanceRecord) {
        int r = userFinanceRecordMapper.updateById(userFinanceRecord);
        if(r > 0){
            r = userRebateMapper.updateById(userRebate);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean userRebateCallback(WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord, WzUserRebate userRebate, List<WzUserCoupon> userCoupons, WzMapRebate mapRebate,WzCouponMarket couponMarket) {
        int r = userFinanceRecordMapper.updateById(userFinanceRecord);
        if(r > 0){
            r = mapRebateMapper.updateById(mapRebate);
            if(r > 0){
                r = userRebateMapper.updateById(userRebate);
                if(r > 0){
                    boolean f = userCouponService.insertBatch(userCoupons);
                    if(f){
                        r = pfFinanceRecordMapper.insert(pfFinanceRecord);
                    }else{
                        r = 0;
                    }
                }
            }
        }
        if(r > 0){
            if(couponMarket != null){
                r = couponMarketMapper.updateById(couponMarket);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }


    @Transactional
    public boolean createRebate(WzUserRebate userRebate, WzUserFinanceRecord userFinanceRecord) {
        int r = userRebateMapper.insert(userRebate);
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean userRebateEnd(WzUser user,WzUserFinanceRecord userFinanceRecord,WzRebateShare rebateShare,WzUserRebate userRebate) {
        int r = userRebateMapper.updateById(userRebate);
        if(r > 0){
            r = userMapper.updateById(user);
            if(r > 0){
                r = rebateShareMapper.insert(rebateShare);
                if(r > 0){
                    r = userFinanceRecordMapper.insert(userFinanceRecord);
                }
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean refund(WzUserFinanceRecord userFinanceRecord, PfFinanceRecord pfFinanceRecord, WzUserCoupon userCoupon,WzCouponMarket couponMarket,WzMapMerge mapMerge,WzMapRebate mapRebate) {
        int r = userCouponMapper.updateById(userCoupon);
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
            if(r > 0){
                r = pfFinanceRecordMapper.insert(pfFinanceRecord);
            }
        }
        if(r > 0){
            if(couponMarket != null){
                r = couponMarketMapper.updateById(couponMarket);
            }
        }
        if(r > 0){
            if(mapMerge != null){
                r = mapMergeMapper.updateById(mapMerge);
            }
        }
        if(r > 0){
            if(mapRebate != null){
                r = mapRebateMapper.updateById(mapRebate);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    public boolean refundProfit(WzUserFinanceRecord userFinanceRecord, WzUserCoupon userCoupon,WzCouponMarket couponMarket,WzMapMerge mapMerge,WzMapRebate mapRebate) {
        int r = userCouponMapper.updateById(userCoupon);
        if(r > 0){
            r = userFinanceRecordMapper.insert(userFinanceRecord);
        }
        if(r > 0){
            if(couponMarket != null){
                r = couponMarketMapper.updateById(couponMarket);
            }
        }
        if(r > 0){
            if(mapMerge != null){
                r = mapMergeMapper.updateById(mapMerge);
            }
        }
        if(r > 0){
            if(mapRebate != null){
                r = mapRebateMapper.updateById(mapRebate);
            }
        }
        if(r > 0){
            return true;
        }else{
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}
